# -*- coding:utf-8 -*-

import logging
import logging.handlers


class MyLogging(logging.Logger):
    def __init__(self, name, path='', level=logging.DEBUG):
        """

        :param name:
        :param path:
        :param level:
        """
        super().__init__(name)
        # self.logger = logging.getLogger()  # 不加名称设置root logger

        self.setLevel(level)
        #formatter = logging.Formatter(
        #    '%(asctime)s.%(msecs)03d:%(name)s:%(funcName)s:%(levelname)s: %(message)s',
        #    datefmt='%y-%m-%d %H:%M:%S')
        formatter = logging.Formatter(
            '%(asctime)s.%(msecs)03d:%(name)s:%(funcName)s:%(lineno)d:%(levelname)s: %(message)s',
            datefmt='%y-%m-%d %H:%M:%S')

        # 使用FileHandler输出到文件
        if path != '':
            print('path=%s' % path)
            import  os
            log_path = os.path.split(path)
            if not os.path.exists(log_path[0]):
                print('log 路径不存在  创建! path=%s' % log_path[0])
                os.makedirs(log_path[0])
            # fh = logging.FileHandler(path)
            """
            根据时间  S/H 秒/小时  24h ，文件不删除backupCount=0

            """
            # fh = logging.handlers.TimedRotatingFileHandler(filename=path, when='h', interval=24,backupCount=0)

            """
            初始化一个轮转文件处理器
            该处理器负责将日志信息写入指定的文件中，并且当文件大小超过10MB时，会自动旋转文件，保留最多50个备份文件  
            """
            fh = logging.handlers.RotatingFileHandler(filename=path, maxBytes=10485760, backupCount=50)

            fh.setLevel(level)
            fh.setFormatter(formatter)
            self.addHandler(fh)

        # 使用StreamHandler输出到屏幕
        ch = logging.StreamHandler()
        ch.setLevel(level)
        ch.setFormatter(formatter)
        self.addHandler(ch)

        # self.debug("This is a debug log.")
        # self.info("This is a info log.")
        # self.warning("This is a warning log.")
        # self.error("This is a error log.")
        # self.critical("This is a critical log.")

    # def setName(self, name):
    #     set_name